/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CONTABILIDAD_CONTROLADOR;

import CONTABILIDAD_MODELO.CON_PARAMETROS_FACTURAS;
import Conexion.Conexion;
import RRHH_Modelo.RHH_USUARIO;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author equipo_4
 */
@WebServlet(name = "CON_PARAMETROS_FACTURAS_CONTROLLER", urlPatterns = {"/CON_PARAMETROS_FACTURAS_CONTROLLER"})
public class CON_PARAMETROS_FACTURAS_CONTROLLER extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ParseException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
      
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ParseException ex) {
            Logger.getLogger(CON_PARAMETROS_FACTURAS_CONTROLLER.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            RHH_USUARIO usuario = ((RHH_USUARIO)request.getSession().getAttribute("usr"));
            Conexion con = new Conexion(usuario.getPASSWORD(), usuario.getPASSWORD());
            String html="";
            String evento = request.getParameter("evento");
                
            switch(evento)
            {
                case "insertar":
                    InsertarparametroFactura(con, request, response);
                    break;
            }
            
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/plain");
            response.getWriter().write(html);
            con.Close();
        } catch (ParseException ex) {
            Logger.getLogger(CON_PARAMETROS_FACTURAS_CONTROLLER.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private String InsertarparametroFactura(Conexion con, HttpServletRequest request, HttpServletResponse response) throws ParseException {
            
            String auto = request.getParameter("auto");
            String vige = request.getParameter("vige");
            String n_ini = request.getParameter("n_ini");
            String nfin = request.getParameter("nfin");
            String llave = request.getParameter("llave");
            String id_old = request.getParameter("id_old");
            
            SimpleDateFormat format = new SimpleDateFormat("YYYYmmDD");
            
            CON_PARAMETROS_FACTURAS cpf = new CON_PARAMETROS_FACTURAS(Double.parseDouble(n_ini), "0", format.parse(vige),auto, Double.parseDouble(nfin), 0, llave);
            cpf.setCon(con);
            cpf.Insertar();
            if(id_old !=null)
            cpf.anular(Double.parseDouble(id_old));
            
            return auto;
    }
}
